package mainpkge;


import game.components.Tank;
import game.components.Balao;
import game.components.Bomba;
import game.components.Tiro;
import java.io.*;
import java.util.Vector;
import javax.microedition.lcdui.*;
import util.Constants;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Bacalá
 */
class ShootDownAirCraft extends Canvas implements Runnable {

    private Balao bola, bola1, bola2;
    private Tank tank;
    private Tiro tiro;
    private Image tirim;
    private Vector bombas = new Vector();
    private int maxX, maxY, fim, pontos = 0, vidas = 3, plane = 0;

    public ShootDownAirCraft() {
        try {
            tirim = Image.createImage("/images/tiro1.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        maxX = getWidth();
        maxY = getHeight();
        
        bola = new Balao(maxX, maxY,10);
        bola1 = new Balao(maxX, maxY,20);
        bola2 = new Balao(maxX, maxY,25);
        
        tank = new Tank(maxX, maxY);
        
        tiro = new Tiro(maxX, maxY);
        tiro.invisible();
        
        //posiciona 3 baloes na parte de cima da tela
        bola.setXI(maxX / 2);
        bola.setYI(maxY / 2 - 40);
        bola.setPassoX(-2);
        
        bola1.setXI(maxX / 3);
        bola1.setYI(maxY / 3 - 30);
        bola1.setPassoX(-1);
        
        bola2.setXI(maxX / 4);
        bola2.setYI(maxY / 4 - 40);
        bola2.setPassoX(-2);
        
        tank.setX(maxX / 2 - 15);
        tank.setY(maxY - 40);        
        
        Thread th = new Thread(this);
        th.start();

    }

    protected void paint(Graphics g) {
        if (this.verQtdDerrubado()) {
            this.youWin(g);
        } else if (tiro.acabouTiro() || vidas <= 0) {
            g.setColor(255, 0, 0);
            if (fim < 2 * maxX | fim < 2 * maxY) {
                fim += 2;
            }
            g.fillArc((maxX - fim) / 2, (maxY - 20 - fim) / 2, fim, fim, 0, 360);
            g.setColor(255, 255, 255);
            g.drawString("GAME OVER", maxX / 2 - 30, (maxY - 55) / 2, Graphics.TOP | Graphics.LEFT);
            g.drawString("PONTOS:" + tiro.pontuacao(), maxX / 2 - 30,
                    (maxY - 25) / 2, Graphics.TOP | Graphics.LEFT);
        } else {
            g.setColor(255, 255, 255);
            g.fillRect(0, 0, getWidth(), getHeight());

            this.planeShoot();

            bola.paint(g);
            bola1.paint(g);
            bola2.paint(g);
            tank.paint(g);
            tiro.paint(g);

            this.paintBombs(g);

            for (int i = 0; i < tiro.getnTiro(); i++) {
                g.drawImage(tirim, 10 + (i * 5), maxY - 20, Graphics.HCENTER | Graphics.VCENTER);
            }
            g.drawString("PT:" + tiro.pontuacao(), maxX - 80, maxY - 30, Graphics.TOP | Graphics.LEFT);
            g.drawString("V:" + vidas, maxX - 40, maxY - 30, Graphics.TOP | Graphics.LEFT);
        }
    }
    
    //se os três aviões forem derrubados 3 vezes, então returna verdadeiro
    public boolean verQtdDerrubado() {
        if (bola.getDerrubado() >= Constants.LEVEL
                && bola1.getDerrubado() >= Constants.LEVEL
                && bola2.getDerrubado() >= Constants.LEVEL) {
            return true;
        }
        return false;
    }
    
    public void planeShoot() {
        Bomba temp;
        if (bombas.size() < Constants.NBOMBS) {
            switch (plane) {
                case 0:
                    temp = new Bomba(maxX, maxY,
                            bola.getX(), bola.getY(), true);
                    if(bola.getDerrubado()>0){
                        temp.setPassoY(bola.getDerrubado());
                    }
                    bombas.addElement(temp);
                    break;
                case 1:
                    temp = new Bomba(maxX, maxY,
                            bola1.getX(), bola1.getY(), true);
                    if(bola1.getDerrubado()>0){
                        temp.setPassoY(bola1.getDerrubado());                        
                    }
                    bombas.addElement(temp);
                    break;
                default:
                    temp = new Bomba(maxX, maxY,
                            bola2.getX(), bola2.getY(), true);
                    if(bola2.getDerrubado()>0){
                        temp.setPassoY(bola2.getDerrubado());                        
                    }
                    bombas.addElement(temp);
            }
            plane++;
            plane = plane % 3;
        }
    }
    
    public void paintBombs(Graphics g){
        for (int i = 0; i < bombas.size(); i++) {
            Bomba bomba = (Bomba) bombas.elementAt(i);
            bomba.paint(g);
        }
    }
    
    public void moveBombs(){
        for(int i = 0; i< bombas.size(); i++){
            Bomba bomba = (Bomba) bombas.elementAt(i);
            bomba.mexe();
            if( (vidas > 0) && bomba.testaColisao(tank) ){
                vidas--;
                System.out.println(vidas);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
            if(!bomba.isVisible()){
                bombas.removeElementAt(i);
            }
        }
    }
    
    private void youWin(Graphics g) {
        g.setColor(0, 255, 0);
        if (fim < 2 * maxX | fim < 2 * maxY) {
            fim += 2;
        }
        g.fillArc((maxX - fim) / 2, (maxY - 20 - fim) / 2, fim, fim, 0, 360);
        g.setColor(255, 255, 255);
        g.drawString("YOU WIN", maxX / 2 - 30, (maxY - 55) / 2, Graphics.TOP | Graphics.LEFT);
        g.drawString("PONTOS:" + tiro.pontuacao(), maxX / 2 - 30,
                (maxY - 25) / 2, Graphics.TOP | Graphics.LEFT);
    }

    public void run() {

        while (true) {
            long inicio = System.currentTimeMillis();
            if (!tiro.acabouTiro()) {
                bola.mexe();
                bola1.mexe();
                bola2.mexe();
                tiro.mexe();
                this.moveBombs();
                bola.testaColisao(tiro);
                bola2.testaColisao(tiro);
                bola1.testaColisao(tiro);
            }
            repaint();
            long fim = System.currentTimeMillis();
            long tempo = fim - inicio;
            if (tempo < 20) {
                try {
                    Thread.sleep(20 - tempo);
                } catch (InterruptedException ie) {
                }
            }

        }

    }

    protected void keyPressed(int t) {
        if (t == KEY_NUM4) {
            tank.mexeE();
        } else if (t == KEY_NUM5) {
            tank.centraliza();
        } else if (t == KEY_NUM6) {
            tank.mexeD();
        } else if (t == KEY_NUM0) {
            if (!tiro.acabouTiro()) {
                tiro.setX(tank.getX());
                tiro.setY(tank.getY());
                tiro.visible();
                tiro.atirou();
            }
        } 
        repaint();
    }

    protected void keyRepeated(int t) {
        if (t == KEY_NUM4) {
            tank.mexeE();
        } else if (t == KEY_NUM5) {
            tank.centraliza();
        } else if (t == KEY_NUM6) {
            tank.mexeD();
        } else if (t == KEY_NUM0) {
            if (!tiro.acabouTiro()) {
                tiro.setX(tank.getX());
                tiro.setY(tank.getY());
                tiro.visible();
                tiro.atirou();
            }
        }
        repaint();
    }
}
